package com.xonro.common.histrans.sfeg.biz.fund;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.UUIDGener;
import com.actionsoft.emm.util.DateUtil;
import com.actionsoft.sdk.local.SDK;
import com.xonro.common.histrans.base.ctx.BOCreateData;
import com.xonro.common.histrans.base.ctx.FileTransData;
import com.xonro.common.histrans.base.ctx.HistoryTransContext;
import com.xonro.common.histrans.base.model.HistoryTransLogModel;
import com.xonro.common.histrans.base.consts.SfegConst;
import com.xonro.common.histrans.sfeg.biz.common.SfegProcessTransBiz;
import com.xonro.common.histrans.sfeg.ctx.SfecProcessCreateContext;
import jodd.util.StringUtil;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class FundApplyBiz extends SfegProcessTransBiz {
    @Override
    protected void transProcess0(SfecProcessCreateContext historyTransContext) {
        String createUser = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getCreateUser();
        String bindId = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getId();
        Timestamp startTime = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getStartTime();
        List<BOCreateData> boCreateDataList = new ArrayList<>();
        RowMap mainData = historyTransContext.getMainData();
        //添加日志
        String mainBOId = UUIDGener.getUUID();
        HistoryTransLogModel logModel = new HistoryTransLogModel();
        historyTransContext.setHistoryTransLogModel(logModel);
        logModel.setBindId(bindId);
        logModel.setBpmBOId(mainBOId);
        logModel.setBpmTable("BO_XR_AM_ASSET_PURCHASE");//资产申请
        logModel.setOuterTable("tb_zcsq");//资产申请
        logModel.setOuterPK(mainData.getString("id"));
        logModel.setOuterExt1(mainData.getString("title"));
        logModel.setOuterExt2(mainData.getString("zclx"));//资产类型
        BOCreateData mainBOCreateData = new BOCreateData();
        boCreateDataList.add(mainBOCreateData);
        BO mainBO = new BO();
        mainBOCreateData.setBo(mainBO);
        mainBOCreateData.setCreateUser(createUser);
        mainBO.setCreateDate(startTime);
        mainBO.setUpdateDate(startTime);
        mainBO.setId(mainBOId);
        mainBO.set("APPLY_NO", mainData.getString("id"));//申请单号
        mainBO.set("TITLE", mainData.getString("title"));//标题
        mainBO.set("PROJECT_NO", mainData.getString("xmbh"));//项目编号

        mainBO.set("CONTENT", mainData.getString("content"));//内容
        mainBO.set("APPLY_DATE",DateUtil.parse(mainData.getString("filldate"),"yyyy-MM-dd"));//标题
        mainBO.set("ASSET_ATTRIBUTE", mainData.getString("zclx"));//资产属性




        List<RowMap> sonDatas= SDK.getCCAPI().getRDSAPI(SfegConst.CCID).getMaps("SELECT * FROM tb_zcsqDetail  WHERE sqid = ?  ",mainData.getString("id"));
        for(RowMap son:sonDatas){
            String uuid = UUIDGener.getUUID();
            BOCreateData subBOCreateData = new BOCreateData();
            boCreateDataList.add(subBOCreateData);
            subBOCreateData.setCreateUser(createUser);
            subBOCreateData.setBoName("BO_XR_AM_ASSET_PURCHASE_LIST");
            BO subBO = new BO();
            subBO.setId(uuid);
            subBOCreateData.setBo(subBO);
            BigDecimal count=new BigDecimal(StringUtil.isNotBlank(mainData.getString("sl"))?mainData.getString("sl"):"0");//数量
            BigDecimal amount=new BigDecimal(StringUtil.isNotBlank(mainData.getString("je"))?mainData.getString("je"):"0");//金额
            BigDecimal price =BigDecimal.ZERO;
            BigDecimal hzPrice =BigDecimal.ZERO;
            if(count.compareTo(BigDecimal.ZERO)!=0) {
                 price = amount.divide(count);
            }
            BigDecimal hzCount=new BigDecimal(StringUtil.isNotBlank(mainData.getString("gzsl"))?mainData.getString("gzsl"):"0");//核准数量
            BigDecimal hzAmount=new BigDecimal(StringUtil.isNotBlank(mainData.getString("gzje"))?mainData.getString("gzje"):"0");//核准金额
            if(hzCount.compareTo(BigDecimal.ZERO)!=0) {
                 hzPrice = hzAmount.divide(hzCount);
            }
            subBO.set("ASSET_NAME",son.getString("sbname"));//物品类型
            subBO.set("LEVEL1_NO",son.getString("sbtype1"));//资产大类
            subBO.set("LEVEL2_NO",son.getString("sbtype2"));//资产小类
            subBO.set("MODEL",son.getString("xxgg"));//型号规格
            subBO.set("UNIT",son.getString("danwei"));//单位
            subBO.set("QUANTITY",son.getString("sl"));//申请数量
            subBO.set("AMOUNT",son.getString("je"));//总价
            subBO.set("PRICE",price);//单价
            subBO.set("REMARK",son.getString("content"));//备注

            subBO.set("ACTUAL_QUANTITY",son.getString("gzsl"));//核准数量
            subBO.set("ACTUAL_PRICE",hzPrice);//核准单价
            subBO.set("ACTUAL_AMOUNT",son.getString("gzje"));//核准总金额

            subBO.set("ACTUAL_ACCEPTANCE_COUNT",son.getString("yssl"));//完成验收数量

            subBO.set("PLAN_NW",son.getString("jhnw"));//计划内外
            subBO.set("REMARK",son.getString("content"));//备注
            subBO.set("REMARK",son.getString("content"));//备注


            super.addLogModel(historyTransContext, "tb_zcsqDetail", son.getString("id"), "BO_XR_AM_ASSET_PURCHASE_LIST", uuid, bindId);

        }
        historyTransContext.getProcessCreateContext().setBoCreateDatas(boCreateDataList);

        //添加附件
        FileTransData fileTransData = new FileTransData();
        fileTransData.setBoId(mainBOId);
        //印章id
        fileTransData.setFkCode(mainData.getString("id"));
        fileTransData.setProcessInstId(bindId);
        historyTransContext.getFileTransDatas().add(fileTransData);
        mainBOCreateData.setBoName("BO_XR_AM_ASSET_PURCHASE");
        fileTransData.setBoName("BO_XR_AM_ASSET_PURCHASE");
        fileTransData.setFieldName("ATTACHMENT");//印张图片

    }

    @Override
    protected String getProcessDefId() {
        return "obj_9a83dd93557d497c914832ffed3adf6d";
    }

    @Override
    protected String getActivityDefId() {
        return "obj_9a83dd93557d497c914832ffed3adf6d";
    }

    @Override
    protected String getTitle(HistoryTransContext ctx) {
        String id = ctx.getMainData().getString("id");//申请单号
        String title = ctx.getMainData().getString("title");//标题
        return id+title;
    }

    @Override
    protected String getEnCode(HistoryTransContext ctx) {
        return ctx.getMainData().getString("id");
    }

    @Override
    protected String getOuterTable(SfecProcessCreateContext historyTransContext) {
        return "tb_zcsq";
    }

    @Override
    protected String getPk(SfecProcessCreateContext historyTransContext) {
        return historyTransContext.getMainData().getString("id");
    }

    @Override
    protected List<RowMap> getMainDatas() {
        return SDK.getCCAPI().getRDSAPI(SfegConst.CCID).getMaps("SELECT  *  FROM tb_zcsq WHERE orcode='1040'");
    }

    @Override
    protected RowMap getMainData() {
        return null;
    }
}
